<!doctype html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>ChucK + JS => ChucKScript</title>
    <link rel="stylesheet" href="../../css/editor.css">
  </head>
  <body onresize="chuckEditor.resize();">
<div>
    <div id="program">// Globals 8: secret replacement dac 2
// This dac is secretly an LPF, and we're fetching its samples
// directly and the main chuck is not actually producing any sound
SawOsc s => dac;
while( true )
{
    Math.random2f( 30, 1000 ) => s.freq;
    130::ms => now;
}
</div>
</div>
<div class="buttons">
    <button class="chuckButton" id="startChuck" disabled="disabled">Start ChucK</button>
    <button class="chuckButton" id="compileButton" disabled="disabled">Compile and Run</button>
    <button class="chuckButton" id="replaceButton" disabled="disabled">Replace</button>
    <button class="chuckButton" id="removeButton" disabled="disabled">Remove</button>
    <button class="chuckButton" id="clearButton" disabled="disabled">Clear VM and Globals</button>
    <button class="chuckButton" id="micButton" disabled="disabled">Enable Microphone</button>
    <button class="chuckButton" id="fileFormButton">Show File Uploader</button>
</div>
<div id="fileForm" class="hidden">
    <p>File name to use in ChucK: <input type="text" id="uploadFilename"></p>
    <p>Local file to upload: <input type="file" id="uploadFile" name="uploadFile"></p>
    <p><button class="chuckButton" id="fileButton" disabled="disabled">Upload File to Virtual File System</button></p>
</div>
<div>
    <textarea id="output"></textarea>
    <div id="shreds">
        <table id="shredstable">
            <col width="60px">
            <col width="1*">
            <col width="50px">
            <col width="60px">
            <tr><th>shred</th><th>code</th><th>time</th><th>remove</th>
        </table>
    </div>
    <div class="clearfix"></div>
</div>
    <script type="text/javascript" src="../../js/ace-min-noconflict/ace.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/editor.js"></script>
    <script type="text/javascript" src="../../js/defer.js"></script>
    <script>
        // override
        var whereIsChuck = "../../js";
    </script>
    <script type="text/javascript" src="../../js/webchuck_host.js"></script>
    <script type="text/javascript" src="../../js/host.js"></script>
    <script>
        theChuckReady.then( async function() 
        {
            // remove compile handlers
            compileButton.removeEventListener( "click", chuckCompileButton );
            replaceButton.removeEventListener( "click", chuckReplaceButton );
            compileButton.disabled = replaceButton.disabled = true;
                        
            // create the secret dac.
            // IMPORTANT: set buffered to true so we can fetch its samples.
            await theChuck.runCode( "global LPF secretDac => blackhole; true => secretDac.buffered; secretDac.freq( 700 );" );

            // create a subinstance based on the secret dac
            var theSubChuckReady = defer();
            var theSubChuck = createASubChuck( theChuck, "secretDac", theSubChuckReady );
            theSubChuck.connect( audioContext.destination );
            await theSubChuckReady;
                    
            // reenable buttons, but with theSubChuck instead of theChuck
            compileButton.addEventListener( "click", function() {
                theSubChuck.runCode( chuckEditor.getValue() );
            });
            
            replaceButton.addEventListener( "click", function() {
                theSubChuck.replaceCode( chuckEditor.getValue() );
            });
            compileButton.disabled = replaceButton.disabled = false;
            
        });
    </script>
  </body>
</html>


